﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace DBTester
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'bBaseDataSet.Students' table. You can move, or remove it, as needed.
            this.studentsTableAdapter1.Fill(this.bBaseDataSet.Students);
            // TODO: This line of code loads data into the 'dBBDataSet.Students' table. You can move, or remove it, as needed.
            //this.studentsTableAdapter.Fill(studentsBindingSource1.DataSource);
            

        }

        private void btnADD_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("Data Source=BJÖRN-DATOR;Initial Catalog=BBase;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
            //SqlConnection cn = new SqlConnection(global::DatabasTest.Properties.Settings.Default.Database1ConnectionString);
            try
            {
                string sqlcommand = "INSERT INTO Students (Id,Name,Age,University) values (" + txtID.Text + ", '" + txtNAME.Text + "', '" + txtAGE.Text + "','" + txtUNI.Text + "')";
                SqlCommand exeSql = new SqlCommand(sqlcommand, cn);
                cn.Open();
                exeSql.ExecuteNonQuery();
                MessageBox.Show("Done");
                this.studentsTableAdapter1.Fill(this.bBaseDataSet.Students);
                txtID.Text = ""; txtNAME.Text = ""; txtAGE.Text = ""; txtUNI.Text = "";
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cn.Close();
            }
        }

        private void btnREF_Click(object sender, EventArgs e)
        {
            this.studentsTableAdapter1.Fill(this.bBaseDataSet.Students);
        }

        private void btnDEL_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("Data Source=BJÖRN-DATOR;Initial Catalog=BBase;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
            //SqlConnection cn = new SqlConnection(global::DatabasTest.Properties.Settings.Default.Database1ConnectionString);
            try
            {
                string sqlcommand = "DELETE FROM Students WHERE ID = " + txtID.Text;
                SqlCommand exeSql = new SqlCommand(sqlcommand, cn);
                cn.Open();
                exeSql.ExecuteNonQuery();
                MessageBox.Show("Done");
                this.studentsTableAdapter1.Fill(this.bBaseDataSet.Students);
                txtID.Text = ""; txtNAME.Text = ""; txtAGE.Text = ""; txtUNI.Text = "";

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cn.Close();
            }
        }
    }
}
